package com.xltx.qq.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xltx.qq.constant.QqConstant;
import com.xltx.qq.model.QqUserInfo;
import com.xltx.qq.properties.QqProperties;
import com.xltx.qq.service.QqOAuth2Service;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * QqOAuth2ServiceImpl
 *
 * @author 乐由
 * @since 2024/5/17 9:26
 */
@Service
public class QqOAuth2ServiceImpl implements QqOAuth2Service {

    @Resource
    private QqProperties qqProperties;

    @Override
    public String authorize() {
        return authorize(UUID.randomUUID().toString());
    }

    @Override
    public String authorize(String state) {
        return authorize(state, "", "");
    }

    @Override
    public String authorize(String state, String scope, String display) {
        return authorize(qqProperties.getAppId(), qqProperties.getRedirectUri(), state, scope, display);
    }

    @Override
    public String authorize(String clientId, String redirectUri, String state, String scope, String display) {
        if ("".equals(clientId) || clientId == null) {
            throw new NullPointerException("appid is null");
        }
        if ("".equals(redirectUri) || redirectUri == null) {
            throw new NullPointerException("redirectUri is null");
        }
        if ("".equals(state) || state == null) {
            throw new NullPointerException("state is null");
        }
        return QqConstant.AUTHORIZE.replace("CLIENT_ID", clientId)
                .replace("REDIRECT_URI", redirectUri)
                .replace("STATE", state)
                .replace("SCOPE", scope)
                .replace("DISPLAY", display);
    }

    @Override
    public JSONObject accessToken(String code) {
        String url = QqConstant.ACCESS_TOKEN.replace("CLIENT_ID", qqProperties.getAppId())
                .replace("CLIENT_SECRET", qqProperties.getAppKey())
                .replace("REDIRECT_URI", qqProperties.getRedirectUri())
                .replace("FMT", "json")
                .replace("NEED_OPENID", "1");
        String result = HttpUtil.get(url);
        if ("".equals(result) || result == null) {
            return null;
        }
        return JSONUtil.parseObj(result);
    }

    @Override
    public JSONObject refreshToken(String refreshToken) {
        String url = QqConstant.REFRESH_TOKEN.replace("CLIENT_ID", qqProperties.getAppId())
                .replace("CLIENT_SECRET", qqProperties.getAppKey())
                .replace("REFRESH_TOKEN", refreshToken)
                .replace("FMT", "json");
        String result = HttpUtil.get(url);
        if ("".equals(result) || result == null) {
            return null;
        }
        return JSONUtil.parseObj(result);
    }

    @Override
    public String acquireOpenId(String accessToken) {
        String url = QqConstant.ACQUIRE_OPEN_ID.replace("ACCESS_TOKEN", accessToken).replace("FMT", "json");
        String result = HttpUtil.get(url);
        if ("".equals(result) || result == null) {
            return null;
        }
        JSONObject object = JSONUtil.parseObj(result);
        return object.getStr("openid");
    }

    @Override
    public QqUserInfo getUserInfo(String accessToken, String openId) {
        String url = QqConstant.GET_USER_INFO.replace("ACCESS_TOKEN", accessToken)
                .replace("APP_ID", qqProperties.getAppId())
                .replace("OPENID", openId);
        String result = HttpUtil.get(url);
        if ("".equals(result) || result == null) {
            return null;
        }
        return JSONUtil.toBean(result, QqUserInfo.class);
    }
}
